package com.sairui.Xiaoruilib;

import android.app.Activity;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.sairui.Xiaoruilib.bean.MoreAskEntity;
import com.sairui.Xiaoruilib.util.GsonConvert;
import com.sairui.Xiaoruilib.util.NetworkUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;

/**
 * Created by sunqiyong on 2018/3/5.
 */

public class MainJsForAndorid {
    private WeakReference<Activity> context;
    private JsListener mJsListener;

    public MainJsForAndorid(WeakReference<Activity> context) {
        this.context = context;
    }

    public void setJsListener(JsListener mJsListener) {
        this.mJsListener = mJsListener;
    }

    @JavascriptInterface
    public void showDetail(final String data){
        context.get().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    String name = jsonObject.getString("chartName");
                    context.get().startActivity(ChartDetailActivity.getInstance(context.get(),name,data,""));
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(context.get(),"数据解析出错",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @JavascriptInterface
    public void readBack(final String data){
        context.get().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //mJsListener.back(data);
            }
        });
    }

    @JavascriptInterface
    public void showMoreAskList(final String data){
        context.get().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(!NetworkUtil.isConnected(context.get())){
                    Toast.makeText(context.get(),"网络不给力，请检查网络设置",Toast.LENGTH_SHORT).show();
                    return;
                }
                MoreAskEntity moreAskEntity = GsonConvert.fromJson(data,new TypeToken<MoreAskEntity>(){}.getType());
                context.get().startActivityForResult(CardOptionActivity.getInstance(context.get(),moreAskEntity),9526);
            }
        });
    }

    @JavascriptInterface
    public void queryChangeChart(){
        context.get().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //mJsListener.change();
            }
        });
    }

    @JavascriptInterface
    public void hideKeyboard(){
        context.get().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mJsListener.hideKeyboard();
            }
        });
    }

    @JavascriptInterface
    public void textToSpeak(final String speakContent){
        context.get().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mJsListener.webSpeak(speakContent);
            }
        });
    }



    public interface JsListener{
        //void back(String s);
        //void change();
        void hideKeyboard();
        void webSpeak(String speak);
    }
}
